<template>
	<form-container
		:title="formData.id ? '编辑编码规则' : '增加编码规则'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="编号" name="ruleCode">
						<a-input v-model:value="formData.ruleCode" placeholder="请输入编号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="名称" name="ruleName">
						<a-input v-model:value="formData.ruleName" placeholder="请输入名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="类型" name="ruleType">
						<a-select
							v-model:value="formData.ruleType"
							show-search
							placeholder="请选择类型"
							:options="ruleTypeOptions"
							@change="onSelectType"
						/> </a-form-item
				></a-col>
				<a-col :span="12">
					<a-form-item label="分类" name="ruleClass">
						<a-select
							v-model:value="formData.ruleClass"
							show-search
							placeholder="请选择分类"
							:options="ruleClassOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="前缀" name="header">
						<a-input v-model:value="formData.header" placeholder="请输入前缀" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="中间时间格式" name="middleTime">
						<a-input
							:disabled="formData.ruleType == 'Global'"
							v-model:value="formData.middleTime"
							placeholder="yyyyMMdd"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="后缀(长度)" name="lastLen">
						<a-input-number v-model:value="formData.lastLen" :min="1" :max="100" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="起始量(含)" name="startValue">
						<a-input-number v-model:value="formData.startValue" :min="1" style="width: 100%" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="rulesServiceForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import rulesApi from '@/api/dev/rulesApi'

	// 抽屉状态
	const visible = ref(false)
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	//表单
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	//提交等待
	const submitLoading = ref(false)

	//类型（数据字典）选项
	const ruleTypeOptions = ref([])
	//分类(数据字典)选项
	const ruleClassOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true //显示抽屉
		//表单数据
		formData.value = {}
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			//const param = {
			//  id: record.id
			//}
			//rulesServiceApi.rulesServiceDetail(param).then((data) => {
			//  formData.value = Object.assign({}, data)
			//  })
		}
		ruleTypeOptions.value = tool.dictList('RULE_TYPE')
		ruleClassOptions.value = tool.dictList('RULE_CLASS')

		if (ruleTypeOptions.value?.length > 0) {
			formData.value['ruleType'] = ruleTypeOptions.value[0].value
		}
		if (ruleClassOptions.value?.length > 0) {
			formData.value['ruleClass'] = ruleClassOptions.value[0].value
		}
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		ruleName: [required('请输入名称')],
		ruleCode: [required('请输入编号（通过编号关联，唯一）')],
		ruleType: [required('请输入类型（数据字典）')],
		ruleClass: [required('请输入分类(数据字典)')],
		lastLen: [required('请输入后缀(长度)')]
	}

	const onSelectType = (e) => {
		switch (e) {
			case 'Day':
				formData.value['middleTime'] = 'yyyyMMdd'
				return
			case 'Week':
				formData.value['middleTime'] = 'yyyyWW'
				return
			case 'Monday':
				formData.value['middleTime'] = 'yyyyMM'
				return
			case 'Year':
				formData.value['middleTime'] = 'yyyy'
				return
			default:
				formData.value['middleTime'] = ''
				return
		}
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			rulesApi
				.submitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
